package com.easy.dn.ui.usercenter.activity;

import com.easy.common.base.BaseActivity;
import com.easy.common.baserx.RxSubscriber;
import com.easy.dn.MainActivity;
import com.easy.dn.api.Api;
import com.easy.dn.api.ApiService;
import com.easy.dn.app.GlobalApplication;
import com.easy.dn.bean.TokenBean;
import com.easy.dn.utils.CommonDataUtil;

import dn.easy.com.easyto.R;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * 公司名称
 */
public class SplashActivity extends BaseActivity {


    @Override
    public int getLayoutId() {
        return R.layout.act_splash;
    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void initView() {
        Api.getInstance()
                .get(ApiService.class)
                .refreshToken(Api.getCacheControl(), GlobalApplication.getInstance().getRefreshToken())
                .map(new Func1<TokenBean, TokenBean>() {
                    @Override
                    public TokenBean call(TokenBean tokenBean) {
                        CommonDataUtil.getJson("position_data.json");
                        return tokenBean;
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new RxSubscriber<TokenBean>(mContext, false) {
                    @Override
                    protected void _onNext(TokenBean tokenBean) {
                        GlobalApplication.getInstance().setAccessToken(tokenBean.getAccess_token());
                        GlobalApplication.getInstance().setRefreshToken(tokenBean.getRefresh_token());
                        startActivity(MainActivity.class);
                        finish();
                    }

                    @Override
                    protected void _onError(String message) {
                        startActivity(LoginActivity.class);
                        finish();
                    }
                });

/*
        //声明WebSettings子类
        WebSettings webSettings = mWebview.getSettings();
        //如果访问的页面中要与Javascript交互，则webview必须设置支持Javascript
        webSettings.setJavaScriptEnabled(true);
        //设置自适应屏幕，两者合用
//        webSettings.setUseWideViewPort(false); //将图片调整到适合webview的大小
//        webSettings.setLoadWithOverviewMode(false); // 缩放至屏幕的大小
//        webSettings.setSupportZoom(true); //支持缩放，默认为true。是下面那个的前提。
//        webSettings.setBuiltInZoomControls(true); //设置内置的缩放控件。若为false，则该WebView不可缩放
//        webSettings.setDisplayZoomControls(false); //隐藏原生的缩放控件
//        webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); //关闭webview中缓存
//        webSettings.setAllowFileAccess(true); //设置可以访问文件
//        webSettings.setJavaScriptCanOpenWindowsAutomatically(true); //支持通过JS打开新窗口
//        webSettings.setLoadsImagesAutomatically(true); //支持自动加载图片
//        webSettings.setDefaultTextEncodingName("utf-8");//设置编码格式
        mWebview.loadUrl("https://qr.alipay.com/bax06247fjtzlzrbwnar002a");
        mWebview.setWebViewClient(new WebViewClient() {
            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                Intent intent = new Intent();
                intent.setData(request.getUrl());
                startActivity(intent);
                return false;
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
//                view.loadUrl(url);
                return false;
            }
        });
        mWebview.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();    //表示等待证书响应
                // handler.cancel();      //表示挂起连接，为默认方式
                // handler.handleMessage(null);    //可做其他处理
            }
        });*/
    }
}
